﻿using Game;

namespace Mekiasm
{
    public class RubberSapl : SimpleSaplItem
    {
        public static int Index;
        
        public RubberSapl() : base( "橡胶树苗", "rubber")
        {
            
        }

        public override void Initilize()
        {
            WoodIndex = RubberWood.Index;
            LeavesIndex = RubberLeaves.Index;
        }
        public override PlantSystem.PlantItem GetPlantItem(int value, int x, int y, int z)
        {
            return new PlantSystem.PlantItem(x, y, z, blockValue, StateDuration, (item, bottomID, hydration, nitrogen) =>
            {
                if (bottomID != SoilValue)
                {
                    item.Remove = true;
                    ILibrary.SubsystemTerrain.ChangeCell(x, y, z, WitheredValue);
                    return;
                }
                if (item.GrowState == MaxState)
                {
                    MekPlantsManager mekPlantsManager = new MekPlantsManager(WoodIndex, LeavesIndex);
                    mekPlantsManager.Paint(ILibrary.SubsystemTerrain, x, y + 1, z);
                    item.Remove = true;
                    int i = 0;
                    while (true)
                    {
                        int v = ILibrary.SubsystemTerrain.Terrain.GetCellValue(x,y,z);
                        if (ILibrary.GetBasicValue(v) == WoodIndex)
                        {
                            //随机贴图位置
                            int pp = ILibrary.random.Int(1, 4);
                            if (random.Bool(0.1f)) ILibrary.SubsystemTerrain.ChangeCell(x, y, z, ILibrary.setExtraData(v, pp));
                        }
                        else break;
                        y++;
                        if (i++ > 10) break;
                    }

                }
            }, (v) =>
            {
                if (ILibrary.GetBasicValue(v) != blockValue && ILibrary.GetBasicValue(v) != WoodIndex)
                {
                    return false;
                }
                return true;
            });
        }

    }
}
